<?php

/**
* Callbacks class
*/
class Callbacks extends Callbacks_Core
{
	function install($params = array())
	{
		$dbconf = array(
			'db_host' => $_SESSION['params']['db_hostname'],
			'db_user' => $_SESSION['params']['db_username'],
			'db_pass' => $_SESSION['params']['db_password'],
			'db_name' => $_SESSION['params']['db_name'],
			'db_encoding' => 'utf8',
		);
		if ( !$this->db_init($dbconf) ) {
			return false;
		}

		$replace = array(
			'{:db_prefix}' => 'my_',
			'{:db_engine}' => in_array('innodb', $this->db_engines) ? 'InnoDB' : 'MyISAM',
			'{:db_charset}' => $this->db_version >= '4.1' ? 'DEFAULT CHARSET=utf8' : '',
			'{:website}' => $_SESSION['params']['virtual_path']
		);
		$file_content = file(BASE_PATH.'sql/data.sql');
		$query = "";
		
		
		$sql = explode(";",file_get_contents(BASE_PATH.'sql/data.sql'));// 
		foreach($sql as $query){
			mysql_query($query);
		}

		$this->db_close();

		$string = '<?php'."\n";;
		$string .= '// ------------------------------------------------------ \\'."\n";
		$string .= '//            You are running FileShareScript V2           \\'."\n";
		$string .= '// ------------------------------------------------------ \\'."\n";	
		$string.= '$DB_HOST= "' . addslashes($_SESSION['params']['db_hostname']) . '";'."\n";		
		$string.= '$DB_USER= "' . addslashes($_SESSION['params']['db_username']) . '";'."\n";
		$string.= '$DB_PASS= "' . addslashes($_SESSION['params']['db_password']) . '";'."\n";
		$string.= '$DB_NAME= "' . addslashes($_SESSION['params']['db_name']) . '";'."\n";
		$string.= "define('INSTALLED','1');\n";		
		$string.= '?>';

		@file_put_contents(rtrim($_SESSION['params']['system_path'], '/').'/secure/connect.php', $string);

		return true;
	}

	function setup_admin($params = array())
	{
		$dbconf = array(
			'db_host' => $_SESSION['params']['db_hostname'],
			'db_user' => $_SESSION['params']['db_username'],
			'db_pass' => $_SESSION['params']['db_password'],
			'db_name' => $_SESSION['params']['db_name'],
			'db_encoding' => 'utf8',
		);
		if ( !($db = $this->db_init($dbconf)) ) {
			return false;
		}

		$this->db_query("INSERT INTO admin (username, password, email, status) VALUES('".$this->db_escape($_SESSION['params']['user_name'])."', '".md5($this->db_escape($_SESSION['params']['user_password']))."', '".$this->db_escape($_SESSION['params']['user_email'])."', '1')");
		
		$this->db_close();

		return true;
	}
	
	function setup_site($params = array())
	{
	
	        $dbconf = array(
			'db_host' => $_SESSION['params']['db_hostname'],
			'db_user' => $_SESSION['params']['db_username'],
			'db_pass' => $_SESSION['params']['db_password'],
			'db_name' => $_SESSION['params']['db_name'],
			'db_encoding' => 'utf8',
		);
		
		$insert = array(
			'site_title' => $_SESSION['params']['site_title'],
			'site_email' => $_SESSION['params']['site_email'],
			'upload_path' => $_SESSION['params']['upload_path'],
			'site_path' => $_SESSION['params']['site_path'],
			'ref_percent' => $_SESSION['params']['ref_percent'],
			'aff_percent' => $_SESSION['params']['aff_percent'],
			'pay_threshold' => $_SESSION['params']['pay_threshold'],
		);
		if ( !($db = $this->db_init($dbconf)) ) {
			return false;
		}

		$this->db_query("INSERT INTO settings (site_title, site_email, upload_path, site_url, referral_percent, user_percent, payment_threshold) VALUES('".$this->db_escape($_SESSION['params']['site_title'])."', '".$this->db_escape($_SESSION['params']['site_email'])."', '".$this->db_escape($_SESSION['params']['upload_path'])."', '".$this->db_escape($_SESSION['params']['site_path'])."', '".$this->db_escape($_SESSION['params']['ref_percent'])."', '".$this->db_escape($_SESSION['params']['aff_percent'])."', '".$this->db_escape($_SESSION['params']['pay_threshold'])."')");

                mkdir($this->db_escape($_SESSION['params']['upload_path'])); 
                chmod($this->db_escape($_SESSION['params']['upload_path']), 0777);

		$this->db_close();

		return true;
	}
}